#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QVBoxLayout>

extern QString tmpDir;

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    installEventFilter(this);

    ui->label_back->setVisible(false);
    ui->combo_backcolor->setVisible(false);
    ui->label_background->setVisible(false);
    ui->label_arect->setVisible(false);
    ui->label_arc->setVisible(false);
    ui->btn_back->setVisible(false);

    ui->label_back->setEnabled(false);
    ui->combo_backcolor->setEnabled(false);
    ui->label_background->setEnabled(false);
    ui->label_arect->setEnabled(false);
    ui->label_arc->setEnabled(false);
    ui->btn_back->setEnabled(false);

    setWindowTitle("MiniDraw");
    // 默认颜色集合，用于背景
    colors1 << Qt::white << Qt::red << Qt::gray << Qt::blue << Qt::yellow << Qt::gray;
    // 默认颜色集合，用户画笔和画刷
    colors2 << Qt::black << Qt::red << Qt::gray << Qt::blue << Qt::yellow << Qt::gray;

    // 初始化颜色下拉框
    initCombo(ui->combo_backcolor, colors1);
    initCombo(ui->combo_pencolor, colors2);
    initCombo(ui->combo_brushcolor, colors1);
    // 其他颜色按钮
    connect(ui->btn_back, SIGNAL(clicked()), this, SLOT(slotOtherColor()));
    connect(ui->btn_pen, SIGNAL(clicked()), this, SLOT(slotOtherColor()));
    connect(ui->btn_brush, SIGNAL(clicked()), this, SLOT(slotOtherColor()));

    // 初始化画笔粗细
    QIntValidator *validator = new QIntValidator(0, 50, this);
    ui->line_pens->setValidator(validator);
    ui->line_pens->setText(QString::number(2));

    // 捕捉鼠标点击事件
    labelList << ui->label_free << ui->label_line << ui->label_arc << ui->label_rect << ui->label_arect \
              << ui->label_polygon << ui->label_ellipse << ui->label_pen \
              << ui->label_back << ui->label_clear << ui->label_save << ui->label_erase ;
    tlList1 << ui->label_free << ui->label_line << ui->label_arc << ui->label_rect << ui->label_arect \
            << ui->label_polygon << ui->label_ellipse << ui->label_pen;
    tlList2 << ui->label_back << ui->label_clear << ui->label_save << ui->label_erase ;
    for(int i=0; i<labelList.length(); i++)
    {
        labelList.at(i)->installEventFilter(this);
    }
    // 画笔粗细改变信号
    connect(ui->line_pens, SIGNAL(textChanged(QString)), this, SLOT(slotChangePenSize(QString)));

    connect(ui->combo_polygontype, SIGNAL(currentIndexChanged(int)), this, SLOT(slotChangePolygonType(int)));
    QList<QString> sList;
    sList << "3" << "4" << "5" << "6" << "7" << "8";
    ui->combo_polygontype->addItems(sList);


    connect(ui->checkBox , SIGNAL(stateChanged(int)), this, SLOT(slotAutoFill(int)));

    backColor = colors1.at(0);
    penColor = colors2.at(0);
    brushColor = colors1.at(0);
    penSize = ui->line_pens->text().toInt();
    // 初始化画图板
    ui->label_draw->setPenSize(penSize);
    ui->label_draw->setPenColor(penColor);
    ui->label_draw->setDrawType(0);
    ui->label_draw->setBackColor(backColor);
    ui->label_draw->setBrushColor(brushColor);
}

void MainWindow::initCombo(QComboBox *comb, QList<QColor> colors)
{
    // 设置背景色为第一个颜色
    QString backColor = "background-color:rgb(%1)";
    QColor color = colors.at(0);
    comb->setStyleSheet(backColor.arg(QString("%1,%2,%3").arg(color.red()).arg(color.green()).arg(color.blue())));
    //必须设置为true，不然QLineEdit为空指针，不能设置styleSheet
    //comb->setEditable(false);
    // 加载项目
    for(int i=0; i<colors.length(); i++)
    {
        comb->addItem("  ");
        //设置项背景色
        comb->setItemData(i,colors[i],Qt::BackgroundRole);
        //设置项字体颜色
        comb->setItemData(i,QColor(Qt::black),Qt::ForegroundRole);
    }
    // 绑定信号
    connect(comb, SIGNAL(currentIndexChanged(int)), this, SLOT(slotChangeColor(int)));
}

bool MainWindow::eventFilter(QObject *obj, QEvent *event)
{
    // 鼠标进入
    if(event->type() == QEvent::Enter)
    {
        for(int i=0; i<tlList2.length(); i++)
        {
            if(obj == tlList2.at(i))
            {
                tlList2.at(i)->setStyleSheet(QString::fromUtf8("border:3px solid blue; border-radius: 10px;"));
            }

        }
        //labelList.at(i)->setStyleSheet(QString::fromUtf8("border:3px solid blue; border-radius: 10px;"));
    }
    // 鼠标离开
    else if (event->type() == QEvent::Leave)
    {
        for(int i=0; i<tlList2.length(); i++)
        {
            if(obj == tlList2.at(i))
            {
                tlList2.at(i)->setStyleSheet(QString::fromUtf8("border:0px solid blue; border-radius: 10px;"));
            }

        }
    }
    // 鼠标单击事件
    else if(event->type() == QEvent::MouseButtonPress)
    {
        QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
        // 左键单击
        if(mouseEvent->button() == Qt::LeftButton)
        {
            for(int i=0; i< labelList.length(); i++)
            {
                if(obj == labelList.at(i))
                {
                    // 修改绘图类型
                    drawType = i;
                    ui->label_draw->setDrawType(drawType);
                }
            }

            for(int i=0; i<tlList1.length(); i++)
            {
                // 修改按钮背景
                if(obj == tlList1.at(i))
                {
                    tlList1.at(i)->setStyleSheet(QString::fromUtf8("border:3px solid blue; border-radius: 10px;"));
                    for(int j=0; j<tlList1.length(); j++)
                    {
                        if(i!=j)
                        {
                            tlList1.at(j)->setStyleSheet(QString::fromUtf8("border:0px solid blue; border-radius: 10px;"));
                        }

                    }
                }
            }
            return true;
        }
        else
        {
            return false;
        }
    }

    if(event->type() == QEvent::FocusOut)
    {
        drawType = -1;
        qDebug() << "focu out";
    }
    return QObject::eventFilter(obj, event);
}

// 选择颜色
void MainWindow::slotChangeColor(int idx)
{
    QString backgroundColor = "background-color:rgb(%1)";
    QColor color;
    // 从下拉框中选择颜色
    if(this->sender() == ui->combo_backcolor)
    {
        color = colors1.at(idx);
        ui->combo_backcolor->setStyleSheet(backgroundColor.arg(QString("%1,%2,%3").arg(color.red()).arg(color.green()).arg(color.blue())));
        backColor = color;
        ui->label_draw->setBackColor(backColor);
    }
    else if(this->sender() == ui->combo_pencolor)
    {
        color = colors2.at(idx);
        ui->combo_pencolor->setStyleSheet(backgroundColor.arg(QString("%1,%2,%3").arg(color.red()).arg(color.green()).arg(color.blue())));
        penColor = color;
        ui->label_draw->setPenColor(penColor);
    }
    else if(this->sender() == ui->combo_brushcolor)
    {
        color = colors1.at(idx);
        ui->combo_brushcolor->setStyleSheet(backgroundColor.arg(QString("%1,%2,%3").arg(color.red()).arg(color.green()).arg(color.blue())));
        brushColor = color;
        ui->label_draw->setBrushColor(brushColor);
    }
}
// 选择其他颜色
void MainWindow::slotOtherColor()
{
    QString backgroundColor = "background-color:rgb(%1)";
    // 从系统对话框中获取颜色
    QColor color = QColorDialog::getColor(QColor(0, 255, 0), this, "选择颜色");
    // 设置颜色
    if(this->sender() == ui->btn_back)
    {
        ui->combo_backcolor->setStyleSheet(backgroundColor.arg(QString("%1,%2,%3").arg(color.red()).arg(color.green()).arg(color.blue())));
        backColor = color;
        ui->label_draw->setBackColor(backColor);
    }
    else if(this->sender() == ui->btn_pen)
    {
        ui->combo_pencolor->setStyleSheet(backgroundColor.arg(QString("%1,%2,%3").arg(color.red()).arg(color.green()).arg(color.blue())));
        penColor = color;
        ui->label_draw->setPenColor(penColor);
    }
    else if(this->sender() == ui->btn_brush)
    {
        ui->combo_brushcolor->setStyleSheet(backgroundColor.arg(QString("%1,%2,%3").arg(color.red()).arg(color.green()).arg(color.blue())));
        brushColor = color;
        ui->label_draw->setBrushColor(brushColor);
    }
}
// 改变画笔粗细
void MainWindow::slotChangePenSize(QString size)
{
    penSize = size.toInt();
    ui->label_draw->setPenSize(penSize);
}
// 改变多边形类型
void MainWindow::slotChangePolygonType(int type)
{
    polygonType = ui->combo_polygontype->currentText().toInt();
    ui->label_draw->setPolygonType(polygonType);
}
void MainWindow::slotAutoFill(int a)
{
    if(ui->checkBox->isChecked())
    {
        ui->label_draw->setAutoFill(1);
    }
    else
    {
        ui->label_draw->setAutoFill(0);
    }
}

// 关闭窗口，删除临时文件
void MainWindow::closeEvent(QCloseEvent *event)
{
    QDir folder(tmpDir);
    if (!folder.exists())
    {
        return;
    }

    QStringList files = folder.entryList(QDir::Files);
    for (const QString& file : files)
    {
        QString filePath = folder.filePath(file);
        QFile::remove(filePath);
    }
}

MainWindow::~MainWindow()
{
    delete ui;
}

